#include <iostream>
#include <queue>

using namespace std;

const int N = 100010;
int n;

int main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;

    long long res = 0;
    priority_queue<long long, vector<long long>, greater<long long>> q;
    for (int i = 0; i < n; ++i) {
        long long x;
        cin >> x;
        q.push(x);
    }
    while (q.size() > 1) {
        long long a = q.top();
        q.pop();
        long long b = q.top();
        q.pop();
        long long sum = a + b;
        res += sum;
        q.push(sum);
    }

    cout << res;
    return 0;
}